
package com.hp

import com.hp.define.ActionMap
import com.hp.define.AppInfo
import com.hp.define.BaseState
import com.hp.define.State
import com.hp.hunter.FileManager
import com.hp.hunter.ImageHunter
import com.hp.upload.Uploader
import javax.servlet.http.HttpServletRequest

public class ActionEnter {
    private HttpServletRequest request = null
    private String rootPath = null // 文件上传根路径
    private String contextPath = null
    private String actionType = null
    private ConfigManager configManager = null

    public ActionEnter(HttpServletRequest request, String uploadPath,String accessPath) {
        this.request = request
        this.rootPath = uploadPath
        this.actionType = request.getParameter("action")
        this.contextPath = request.getContextPath()
        this.configManager = ConfigManager.getInstance(this.rootPath, this.contextPath, request.getRequestURI(),accessPath)
    }

    public String exec() {
        String callbackName = this.request.getParameter("callback")
        if (callbackName != null) {
            return !this.validCallbackName(callbackName) ? (new BaseState(false, AppInfo.ERROR_401_ILLEGAL)).toJSONString() : callbackName + "(" + this.invoke() + ")"
        } else {
            return this.invoke()
        }
    }

    public String invoke() {
        if (this.actionType != null && ActionMap.mapping.containsKey(this.actionType)) {
            if (this.configManager != null && this.configManager.valid()) {
                State state = null
                int actionCode = ActionMap.getType(this.actionType)
                Map<String, Object> conf = null
                switch(actionCode) {
                    case ActionMap.CONFIG:
                        return this.configManager.getAllConfig().toString()
                    case ActionMap.UPLOAD_IMAGE:
                    case ActionMap.UPLOAD_SCRAWL:
                    case ActionMap.UPLOAD_VIDEO:
                    case ActionMap.UPLOAD_FILE:
                        conf = this.configManager.getConfig(actionCode)
                        state = (new Uploader(this.request, conf)).doExec()
                        break
                    case ActionMap.CATCH_IMAGE:
                        conf = this.configManager.getConfig(actionCode)
                        String[] list = this.request.getParameterValues((String)conf.get("fieldName"))
                        state = (new ImageHunter(conf)).capture(list)
                        break
                    case ActionMap.LIST_FILE:
                    case ActionMap.LIST_IMAGE:
                        conf = this.configManager.getConfig(actionCode)
                        int start = this.getStartIndex()
                        state = (new FileManager(conf)).listFile(start)
                }

                return state.toJSONString()
            } else {
                return (new BaseState(false, AppInfo.ERROR_102_CONFIG_ERROR)).toJSONString()
            }
        } else {
            return (new BaseState(false, AppInfo.ERROR_101_INVALID_ACTION)).toJSONString()
        }
    }

    public int getStartIndex() {
        String start = this.request.getParameter("start")

        try {
            return Integer.parseInt(start)
        } catch (Exception var3) {
            return 0
        }
    }

    public boolean validCallbackName(String name) {
        return name.matches("^[a-zA-Z_]+[\\w0-9_]*\$")
    }
}
